package com.qf.eduadmin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.eduadmin.mapper.QfCourseClassifyDao;
import com.qf.eduadmin.mapper.QfCourseDao;
import com.qf.eduadmin.pojo.vo.QfCourse;
import com.qf.eduadmin.pojo.vo.QfCourseClassify;
import com.qf.eduadmin.pojo.resp.ResponseData;
import com.qf.eduadmin.service.CourseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Mr.Liu
 * @date 2021/12/23 20:09
 */
@Service
public class CourseServiceImpl implements CourseService {
    @Resource
    private QfCourseDao qfCourseDao;
    @Resource
    private QfCourseClassifyDao qfCourseClassifyDao;
    @Override
    public ResponseData findAll(Integer page,Integer limit) {
        PageHelper.startPage(page,limit);
        List<QfCourse> qfCourses = qfCourseDao.queryAllByLimit();
        PageInfo<QfCourse> courses = PageInfo.of(qfCourses);
        return new ResponseData(0,"查询成功",courses.getList(),courses.getTotal());
    }
    //添加课程，1.将课程信息添加到qf_course表中。
    //2.将新增得课程Id 与 课程分类信息存储在  qf_course_classfiy中
    @Transactional//设置事务，如果任意一个新增方法失败，则进行回滚
    @Override
    public ResponseData insertCourse(QfCourse qfCourse) {
        //添加课程时，需要将刚刚新增得id返回
        Integer i = qfCourseDao.insertCourse(qfCourse);
        //新增得课程id
        if(i!=null){
            System.out.println(qfCourse.getId());
            QfCourseClassify qfCourseClassify = new QfCourseClassify();
            qfCourseClassify.setClassifyId(qfCourse.getClassfiyId());
            qfCourseClassify.setCourseId(qfCourse.getId());
            //执行中间表得添加
            qfCourseClassifyDao.insert(qfCourseClassify);
            //返回新增成功
            return new ResponseData(0,"新增成功",null,null);
        }
        return null;
    }

    @Override
    public ResponseData findById(Integer id) {
        QfCourse qfCourse = qfCourseDao.findById(id);
        //System.out.println(qfCourse);
        if(qfCourse!=null){
            return new ResponseData(0,"查询成功",qfCourse,null);
        }
        return new ResponseData(1,"查询失败",null,null);
    }

    @Override
    public ResponseData updateCourse(QfCourse qfCourse) {
        //修改两张表
        Integer integer = qfCourseDao.updateCourse(qfCourse);
        QfCourseClassify qfCourseClassify = new QfCourseClassify();
        //首先通过课程Id查询当前课程所属得分类
        qfCourseClassify.setCourseId(qfCourse.getId());
        //获取到该课程所属的分类
        List<QfCourseClassify> qfCourseClassifies = qfCourseClassifyDao.queryAll(qfCourseClassify);
        //System.out.println(qfCourseClassifies);
        //获取到该课程的分类信息
        QfCourseClassify qfCourseClassify1 = qfCourseClassifies.get(0);
        //System.out.println(qfCourseClassify1);
        //设置该课程新的分类
        qfCourseClassify1.setClassifyId(qfCourse.getClassfiyId());
        int update = qfCourseClassifyDao.update(qfCourseClassify1);
        if(integer!=null && update>0){
            return new ResponseData(0,"修改成功",null,null);
        }
        return null;
    }
}
